package com.lb.statck.filter;


import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


import javax.servlet.http.Cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

@Slf4j
@Component
public class LoginFilter implements jakarta.servlet.Filter {








    @Override
    public void doFilter(jakarta.servlet.ServletRequest servletRequest, jakarta.servlet.ServletResponse servletResponse, jakarta.servlet.FilterChain filterChain) throws IOException, jakarta.servlet.ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        boolean b = request.getRequestURI().endsWith("/api/user/h5login");
        // 检查是否是登录请求
        if (request.getRequestURI().endsWith("/api/user/h5login")) {
            // 处理登录逻辑
            // String username = request.getParameter("username");
            //  String password = request.getParameter("password");
            filterChain.doFilter(request, response);

        }
        if (request.getRequestURI().endsWith("/api/email/send")) {
            // 检查是否是发送验证码
            // String username = request.getParameter("username");
            //  String password = request.getParameter("password");
            filterChain.doFilter(request, response);

        }
else {
         //  HttpSession session = request.getSession(false); // 不创建新的session

         //   request.getSession().getAttribute("user");
            // 检查用户是否已登录
           request.getCookies();
           log.info(Arrays.toString(request.getCookies())
);
            HttpSession session = request.getSession(false); // 不创建新的session
            if (session != null) {
              Object  user = session.getAttribute("user");
                if (user == null) {
                    // filterChain.doFilter(request, response);
                    response.setContentType("application/json;charset=UTF-8");
                    PrintWriter out = response.getWriter();
                    out.print("{\"status\":\"unauthorized\",\"message\":\"用户未登录，请登录后再试\"}");
                    out.flush();
                    out.close();
                    return;
                    //  response.sendRedirect("http://localhost:8080/#/pages/code/code");
                }
                else {
                    filterChain.doFilter(request, response);
                }
            }
          else {   response.setContentType("application/json;charset=UTF-8");
                PrintWriter out = response.getWriter();
                out.print("{\"status\":\"unauthorized\",\"message\":\"用户未登录，请登录后再试\"}");
                out.flush();
                out.close();
                // 用户已登录，继续处理请求
             //   filterChain.doFilter(request, response);
            }
        }
        }

    @Override
    public void destroy() {
        // 销毁逻辑（可选）
    }
}